// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import PetEntity from "../../Models/Pets/PetEntity";
import PetManager from "../../Models/Pets/PetManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PetItem extends cc.Component {

    @property(cc.Label)
    nameTxt: cc.Label = null;
    @property(cc.Label)
    lvTxt: cc.Label = null;
    @property(cc.Toggle)
    bgToggle: cc.Toggle = null;
    // @property(cc.Label)
    // attributeTxt0: cc.Label = null;
    // @property(cc.Label)
    // attributeTxt1: cc.Label = null;
    // @property(cc.Button)
    // takeoffBtn: cc.Button = null;
    // @property(cc.Button)
    // wearBtn: cc.Button = null;
    // @property(cc.Button)
    // jinhuaBtn: cc.Button = null;
    @property(cc.Node)
    lockNode: cc.Node = null;
    @property(cc.Label)
    unlockTipsTxt: cc.Label = null;
    @property(cc.ProgressBar)
    expProgressbar: cc.ProgressBar = null;
    @property(cc.Node)
    selectNode: cc.Node = null;
    @property(cc.Node)
    fightIngNode: cc.Node = null;
    @property(sp.Skeleton)
    petSkeleton: sp.Skeleton = null;

    petEy: PetEntity;
    itemClickFun: Function = null;
    itemCaller: any = null;

    drawItem(ey: PetEntity, i: number = 0, unlockid: number = -1) {
        this.petEy = ey;
        this.lockNode.active = unlockid == -1;
        this.bgToggle.isChecked = unlockid != -1;
        this.unlockTipsTxt.string = "@关".replace("@", PetManager.single.petWearUnlockConfig[i] + "");
        if (this.petEy != null) {
            this.nameTxt.string = this.petEy.petTp.petName;
            this.lvTxt.string = "等级" + this.petEy.level;
            this.expProgressbar.progress = this.petEy.exp / this.petEy.upgradeExp;
            this.fightIngNode.active = this.petEy.isFight;
            cc.resources.load("pets/" + this.petEy.petTp.petSkelentonName, sp.SkeletonData, (error: Error, assets: sp.SkeletonData) => {
                this.petSkeleton.skeletonData = assets;
                this.petSkeleton.setAnimation(0, "idle1", true);
            })
            // this.takeoffBtn.node.active = this.petEy.isFight && this.petEy.level < this.petEy.maxLevel;
            // this.jinhuaBtn.node.active = this.petEy.isFight && this.petEy.level >= this.petEy.maxLevel;
            // this.wearBtn.node.active = !this.petEy.isFight;
            // this.attributeTxt0.string = this.petEy.attributeDes0;
            // this.attributeTxt1.string = this.petEy.attributeDes1;
        } else {
            this.nameTxt.string = "";
            this.lvTxt.string = "";
            this.fightIngNode.active = false;
            this.petSkeleton.skeletonData = null;
            // this.jinhuaBtn.node.active = this.wearBtn.node.active = this.takeoffBtn.node.active = false;
            // this.attributeTxt0.string = "?";
            // this.attributeTxt1.string = "?";

        }


    }

    takeoffClick() {
        if (!this.petEy) {
            return;
        }
        PetManager.single.requestPetTakeoff(this.petEy.id);
    }

    wearClick() {
        if (!this.petEy) {
            return;
        }

        PetManager.single.requestPetFight(this.petEy.id);
    }

    petItemClick() {
        this.itemClickFun?.call(this.itemCaller, this);
    }

    jinhuaClick() {

    }
}
